package com.zx.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

/**
 * @author ZX
 * @date 2021/4/14
 */
public class JwtUtils
{

    public static final String SIGN = "!@#$%qwertyaq1@";

    /**
     * 生成token
     * @param map
     * @return
     */
    public static String getToken(Map<String, String> map)
    {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DATE, 7);

        JWTCreator.Builder builder = JWT.create();
        map.forEach((k, v) -> {
            builder.withClaim(k, v);
        });
        String token = builder.withExpiresAt(instance.getTime()).sign(Algorithm.HMAC256(SIGN));

        return token;
    }

    /**
     * 验证Token
     * @param token
     * @return
     */
    public static DecodedJWT  verify(String token){

        DecodedJWT decodedJWT = JWT.require(Algorithm.HMAC256(SIGN)).build().verify(token);

        return  decodedJWT;
    }

}
